﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace SharePointUtilitiesForDevelopers
{
    /// <summary>
    /// A domain class that represents a SharePoint feature definition.
    /// </summary>
    [ReadOnly(true)]
    public class SharePointFeature
    {
        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the version.
        /// </summary>
        /// <value>The version.</value>
        public string Version { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="SharePointFeature"/> is hidden.
        /// </summary>
        /// <value><c>true</c> if hidden; otherwise, <c>false</c>.</value>
        public bool Hidden { get; set; }

        /// <summary>
        /// Gets or sets the scope.
        /// </summary>
        /// <value>The scope.</value>
        public string Scope { get; set; }

        /// <summary>
        /// Gets or sets the solution id.
        /// </summary>
        /// <value>The solution id.</value>
        public string SolutionId { get; set; }

        /// <summary>
        /// Gets or sets the element manifests.
        /// </summary>
        /// <value>The element manifests.</value>
        public List<SharePointFeatureElementManifest> ElementManifests { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SharePointFeature"/> class.
        /// </summary>
        public SharePointFeature()
        {
            Version = "12.0.0.0";
            Scope = "Site";
            Id = Guid.NewGuid();
            Hidden = false;
            ElementManifests = new List<SharePointFeatureElementManifest>();
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <returns></returns>
        public string GetXml()
        {
            // CreateInstance feature
            var featureElement = new XElement(
                "{http://schemas.microsoft.com/sharepoint/}Feature",
                new XAttribute("Id", Id.ToString()),
                new XAttribute("Title", Title),
                new XAttribute("Description", Description),
                new XAttribute("Version", Version),
                new XAttribute("Hidden", Hidden.ToString().ToUpperInvariant()));
                    
            if(ElementManifests.Count > 0)
            {
                var manifestsElement = new XElement("ElementManifests");

                foreach(var manifest in ElementManifests)
                {
                    var manifestElement = new XElement("ElementManifest", new XAttribute("Location", manifest.Location));
                    manifestsElement.Add(manifestElement);
                }
                featureElement.Add(manifestsElement);
            }
            
            var sb = new StringBuilder();
            var xws = new XmlWriterSettings {OmitXmlDeclaration = true, Encoding = Encoding.UTF8};
            
            using (var writer = XmlWriter.Create(sb, xws))
            {
                if (writer != null) featureElement.Save(writer);
            }

            return sb.ToString();
            
        }
    }
}